package com.mach4.gamesuite;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.skiller.api.listeners.SKListenerInterface;
import com.skiller.api.operations.SKApplication;
import com.skiller.api.operations.SKUIManager.eScreenType;
import com.skiller.api.responses.SKStatusResponse;
import com.skiller.api.responses.SKStatusResponse.eResponseStatus;
import com.skiller.api.responses.SKTurnbasedGameChosenResponse;

public class MainActivity extends Activity 
{	
	MainActivity mContext = this;
	
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        String applicationId = "445599865701";
        String applicationKey = "249f292c6e41462a8c8bdd6e3e88347b";
        String applicationSecret = "a0412b88aa324b31975de57a5b7487fd";
		String applicationVersion = "1";
		int    distributorId = 7; //(7 = Play - Android Market)
	
		//initialize the application
		SKApplication.getInstance().init(mContext, applicationId, applicationKey, applicationSecret, applicationVersion, distributorId);
		
		//sign in the user
		SKApplication.getInstance().login(mContext, new SKListenerInterface<SKStatusResponse>() 
		{
			@Override
			public void onResponse(SKStatusResponse response)
			{
				//User signed in successfully
			}
	
			@Override
			public void onError(SKStatusResponse response) 
			{
				Toast toast = Toast.makeText(mContext, "You need to sign in to get social.", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.BOTTOM | Gravity.LEFT, 0, 0);
				toast.show();
			}
		});
		    
		//create battleship game
	    Button createBattleship = (Button) findViewById(R.id.createBattleship);
	    createBattleship.setOnClickListener(new OnClickListener() 
		{
			public void onClick(View v) 
			{	
			    //BattleshipManager.getInstance().createGame();			    
			    Intent intent = new Intent(mContext, BattleshipActivity.class );
			    intent.putExtra("player_intent", "create");
			    mContext.startActivity(intent);
			}
		});
	    
	    //create tictactoe game
        Button createTicTacToe = (Button) findViewById(R.id.createTicTacToe);
        createTicTacToe.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {   
                TicTacToeManager.getInstance().createGame();               
                Intent intent = new Intent(mContext, TicTacToeActivity.class );
                mContext.startActivity(intent);
            }
        });
	    
	    //join existing game
		Button joinGameButton = (Button) findViewById(R.id.JoinGame);
	    joinGameButton.setOnClickListener(new OnClickListener() 
		{
			public void onClick(View v) 
			{				
				SKApplication.getInstance().getGameManager().getTurnBasedTools().showGamesLobby(mContext, new SKListenerInterface<SKTurnbasedGameChosenResponse>()
				{
					@Override
					public void onResponse(SKTurnbasedGameChosenResponse response)
					{
						Log.w("MainActivity", "onResponse");
						Log.w("Joining game", response.getSettings());
						
					    if(response.getSettings().equals("Battleship"))
					    {
					    	Log.w("Join Game", "Joining Battleship");
					        //BattleshipManager.getInstance().joinGame(response.getGameId());						
					        Intent intent = new Intent(mContext, BattleshipActivity.class);
	                        intent.putExtra("player_intent", response.getGameId());
					        mContext.startActivity(intent); 
					    }
					    else if(response.getSettings().equals("TicTacToe"))
					    {
					    	Log.w("Join Game", "Joining Tic Tac Toe");
					        TicTacToeManager.getInstance().joinGame(response.getGameId());
					        Intent intent = new Intent(mContext, TicTacToeActivity.class);
	                        mContext.startActivity(intent); 
					    }
					    else
					    {
					    	Log.w("Join Game", "Not joining valid game");
					    }
					}

					@Override
					public void onError(SKStatusResponse response) 
					{
						if (response.getStatusCode() != eResponseStatus.USER_CANCELED.getCode())
						{
							//If the user did not cancel the operation you can handle the error here
						}
					}
				});				
			}
		});

	 	//show social dashboard
		Button goSocialButton = (Button) findViewById(R.id.GoSocial);
	    goSocialButton.setOnClickListener(new OnClickListener() 
		{
			public void onClick(View v) 
			{				
				SKApplication.getInstance().getUIManager().showScreen(mContext, eScreenType.DASHBOARD);
			}
		});
    }
    
	@Override
	public void onDestroy()
	{
		android.os.Process.killProcess(android.os.Process.myPid());
	}
}